//
//  SEGTrackPayView.m
//  Uhomebk
//
//  Created by mac on 2017/10/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "SEGTrackPayView.h"
#import "SEGPayPriceListController.h"
#import "SEGTrackMatrelsController.h"
#define kVlineViewX 69.5
#define kVlineViewWidth 1
@implementation SEGTrackPayView

-(instancetype)initWithFrame:(CGRect)frame trackModel:(SEGOrderTrackInfoModel *)trackModel
{
    if (self = [super initWithFrame:frame]) {
        [self setupUIWithFrame:frame trackModel:trackModel];
        
    }
    return self;
}

/**
 UI布局
  */
-(void)setupUIWithFrame:(CGRect)frame trackModel:(SEGOrderTrackInfoModel *)trackModel
{
    UIView *trackOtherView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0)];
    trackOtherView.backgroundColor = kColor_W;
    [self addSubview:trackOtherView];
    CGFloat otherViewHeight = 0;
    //2.1子任务节点标识
    /* 节点名字*/
    UILabel *otherNameIcon = [[UILabel alloc] initWithFrame:CGRectMake(kVlineViewX+15, 12, 33, 15)];
    otherNameIcon.font = kFont_11;
    [otherNameIcon setTextColor:kColor_2];
    otherNameIcon.backgroundColor = kColor_bg;
    otherNameIcon.layer.cornerRadius = 8;
    otherNameIcon.layer.masksToBounds = YES;
    otherNameIcon.textAlignment = NSTextAlignmentCenter;
    otherNameIcon.text = @"有偿";
    /* 竖线*/
    UIView *vLineView = [[UIView alloc] initWithFrame:CGRectMake(kVlineViewX, 0, 1, 0)];
    vLineView.backgroundColor = kColor_L2;
    /* 有偿服务内容*/
    //计算文案宽度
//    NSString *serviceStr = [NSString stringWithFormat:@"%@ 选择了有偿服务，",[self getCurrUserName:trackModel.currentUser]];
    NSString *serviceStr = @"确认了有偿服务";
    CGSize serviceLabelSize = STRING_SIZE_FONT(1000, serviceStr, 15);
    UILabel *serviceLabel = [[UILabel alloc] initWithFrame:CGRectMake(kVlineViewX+15, CGRectGetMaxY(otherNameIcon.frame)+12, serviceLabelSize.width, 15)];
    [serviceLabel setTextColor:kColor_1];
     serviceLabel.font = kFont_15;
    serviceLabel.text = serviceStr;
    /* 查看有偿服务按钮*/
    CustomButtonView *serviceButton = [[CustomButtonView alloc]initWithFrame:CGRectMake(kVlineViewX + 5, CGRectGetMaxY(serviceLabel.frame)+12, 80, 15)];
    serviceButton.tempData = trackModel.payServices;
    [serviceButton setTitle:@"服务清单" forState:UIControlStateNormal];
    [serviceButton setTitleColor:kColor_S3 forState:UIControlStateNormal];
    serviceButton.titleLabel.font = kFont_15;
    [serviceButton addTarget:self action:@selector(checkServiceAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *materelsIcon;
    UILabel *materelsLabel;
    CustomButtonView *materelsButton;
    
    //有物料信息
    if (trackModel.materiels.count > 0) {
        /* 物料信息内容*/
        
        /* 节点名字*/
        materelsIcon = [[UILabel alloc] initWithFrame:CGRectMake(kVlineViewX+15, CGRectGetMaxY(serviceButton.frame)+12, 33, 15)];
        materelsIcon.font = kFont_11;
        [materelsIcon setTextColor:kColor_2];
        materelsIcon.backgroundColor = kColor_bg;
        materelsIcon.layer.cornerRadius = 8;
        materelsIcon.layer.masksToBounds = YES;
        materelsIcon.textAlignment = NSTextAlignmentCenter;
        materelsIcon.text = @"物料";
        //计算文案宽度
        NSString *materelsStr = @"确认了物料";
        CGSize materelsLabelSize = STRING_SIZE_FONT(1000, materelsStr, 15);
        materelsLabel = [[UILabel alloc] initWithFrame:CGRectMake(kVlineViewX+15, CGRectGetMaxY(materelsIcon.frame)+12, materelsLabelSize.width, 15)];
        [materelsLabel setTextColor:kColor_1];
        materelsLabel.font = kFont_15;
        materelsLabel.text = materelsStr;
        /* 查看物料清单按钮*/
        materelsButton = [[CustomButtonView alloc]initWithFrame:CGRectMake(kVlineViewX+5, CGRectGetMaxY(materelsLabel.frame)+12, 80, 15)];
        [materelsButton setTitle:@"物料清单" forState:UIControlStateNormal];
        [materelsButton setTitleColor:kColor_S3 forState:UIControlStateNormal];
        materelsButton.titleLabel.font = kFont_15;
        materelsButton.tempData  = trackModel.materiels;
        [materelsButton addTarget:self action:@selector(checkMaterelsAction:) forControlEvents:UIControlEventTouchUpInside];
    }
 
    [trackOtherView addSubview:vLineView];
    [trackOtherView addSubview:otherNameIcon];
    [trackOtherView addSubview:serviceLabel];
    [trackOtherView addSubview:serviceButton];
    if (trackModel.materiels.count > 0) {
        [trackOtherView addSubview:materelsIcon];
        [trackOtherView addSubview:materelsLabel];
        [trackOtherView addSubview:materelsButton];
    }
    
    CGFloat priceY = trackModel.materiels.count > 0?CGRectGetMaxY(materelsButton.frame)+12:CGRectGetMaxY(serviceButton.frame)+12;
    /* 总价*/
    UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(kVlineViewX+15, priceY, self.frame.size.width-kVlineViewX-30, 15)];
    [priceLabel setTextColor:kColor_1];
    priceLabel.font = kFont_15;
    priceLabel.text = [NSString stringWithFormat:@"总计：%.2f元",[trackModel.charge floatValue]/100];
    
    //重新适配高度
    otherViewHeight = CGRectGetMaxY(priceLabel.frame);
    [trackOtherView addSubview:priceLabel];
    
    vLineView.height = otherViewHeight;
    self.height = otherViewHeight;
    trackOtherView.height = otherViewHeight;
}

-(NSString *)getCurrUserName:(NSArray *)userArray
{
    if (userArray.count == 0) {
        return @"";
    }
    __block NSString *currUser = @"";
    [userArray enumerateObjectsUsingBlock:^(SEGOrderCurrentUser * _Nonnull userModel, NSUInteger idx, BOOL * _Nonnull stop) {
        if (userArray.count > 1  ) {
            if (userModel.userName.length>0) {
                if (idx == 0) {
                    currUser = userModel.userName;
                }
                else{
                    currUser = [currUser stringByAppendingString:[NSString stringWithFormat:@",%@",userModel.userName]];
                }
                
            }
        }
        else{
            currUser = userModel.userName;
        }
    }];
    return currUser;
}

-(void)checkServiceAction:(CustomButtonView *)sender
{
    SEGPayPriceListController *payPriceVC = [[SEGPayPriceListController alloc] init];
    payPriceVC.dataArray = sender.tempData;
    [self.parentVC.navigationController pushViewController:payPriceVC animated:YES];
}

/**
 查看物料清单
 */
-(void)checkMaterelsAction:(CustomButtonView *)sender
{
    SEGTrackMatrelsController *VC = [[SEGTrackMatrelsController alloc] init];
    VC.dataArray = sender.tempData;
    [self.parentVC.navigationController pushViewController:VC animated:YES];
}

@end
